<?php

namespace app\pay\controller;

use think\Controller;
use think\Request;



class Weixin extends Controller {


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->assign('v',1);
    }

    public function pay($id) {

	    if(!$id){
            $id = Request::instance()->get('id');
        }

        $order = model('order')->where('id',$id )->find();

        if(!$order){
            $this->error('没有找到订单！');
        }

		if($order->status==0){
             $user = model('user')->where('uid',$order->buyUid)->find();
             $this->assign('user',$user);
             $this->assign('order',$order);

             return view('pay');
        }
		else{
            $this->error('订单已支付！');
        }

	}

//    余额支付
	public function pay3($id){
        if(!$id){
            $id = Request::instance()->get('id');
        }

        $order = model('order')->where('id',$id )->find();

        if(!$order){
            $this->error('没有找到订单！');
        }

        if($order->status==0){
            //余额
            $user = model('user')->where('uid',$order->buyUid)->find();
            $bag = $user->money;
            $fee = $order->totalFee;
            $result = $bag-$fee;
            if($result<0){
                $this->error('余额不足！');
            }
            $user->money = $result;
            $user->save();
            //订单状态

            if($order->otype==1){
                $order->status = 3;
                $product = model('recharge')->where('id',$order->productId)->find();
            }
            else{
                $order->status = 1;
                $product = model('product')->where('id',$order->productId)->find();
            }
            $order->payTime = time();
            $order->sendTime = time();
            $order->payWay = '余额支付';
            $order->save();

            //销量

            $num = $order->num;
            $product->saleNum = ($product->saleNum) + $num;
            $product->save();


            //处理积分
            $credit = ($order->totalFee)/100*getSettingValue('consume_credit');
            if($credit){
                $user->credit = $user->credit + $credit;
                $user->save();
                $history = [
                    'uid'=>$user->uid,
                    'num'=>$credit,
                    'source'=>'消费获得',
                    'cate'=>1,
                    'createTime'=>time(),
                ];
                model('credit')->insertGetId($history);

            }
            $this->redirect('pResult');
        }
        else{
            $this->error('订单已支付！');
        }

    }

//	微信支付
    public function pay2($id) {

        require_once('weixinpay/WxPayData.class.php');
        require_once('weixinpay/WxPayApi.class.php');
        require_once('weixinpay/WxPayJsApiPay.php');

        $order = model('order')->where('id',$id )->find();
        get_pay_config();
        if(!$order){
            $this->error('没有找到订单！');
        }
        $jsApiParameters='';
        if($order->otype==0){
            $product = model('product')->where('id',$order->productId)->find();
        }
        elseif($order->otype==1){
            $product = model('recharge')->where('id',$order->productId)->find();
        }

        if($order->status==0){
            {
                $tools = new \JsApiPay ();
                $openId = $tools->GetOpenid();
                if(!$openId){
                    $this->redirect('pay',['id'=>$id]) ;
                }
                // 统一下单
                $input = new \WxPayUnifiedOrder ();
                $input->SetBody ( $product->title );
                $input->SetOut_trade_no ( $order->payment_order_id  );//shop_order表的payment_order_id
                $input->SetTotal_fee ( $order->totalFee );
                $input->SetNotify_url ( 'http://99.qikes.cn/public/pay/weixin/notify' );
                $input->SetTrade_type ( "JSAPI" );
                $input->SetOpenid ( $openId );
                $result = \WxPayApi::unifiedOrder ( $input );

                if ($result ['return_code'] == 'FAIL') {
                    echo  $result ['return_msg'];
                    exit ();
                }
                if($result['result_code'] == 'FAIL'){
                    //商户订单号重复

                    $new_payment_order_id = date('YmdHis').rand(1000,9999) ;
                    model('order')->where('payment_order_id',$order->payment_order_id )->update(['payment_order_id'=>$new_payment_order_id]);
                    $this->redirect('pay',['id'=>$id]);
                }
                $jsApiParameters = $tools->GetJsApiParameters ( $result );//appId,nonceStr,package,signType,timeStamp,paySign
                $this->assign('jsApiParameters',$jsApiParameters);
            }

        }

        if(!$jsApiParameters){
            $this->redirect('pay',['id'=>$id]) ;
        }

        $this->assign('product',$product);
        $this->assign('order',$order);
        return view('pay2');
    }



    public function notify(){

        require_once('weixinpay/notify.php');
        $notify = new \PayNotifyCallBack();
        $notify->Handle(false);

    }





    public function pResult(){

        return view('pResult');

    }




}
